//
//  M4HisRepayCell.swift
//  M4
//
//  Created by YuChuang on 2024/8/22.
//

import UIKit

enum M4HistoryRepayAction: Int {
    case M4HisPagaAction = 0
    case M4ProrrogaAction = 1
    case M4HisCellAction = 2
}

class M4HisRepayCell: M4BaseTableViewCell {
    
    @IBOutlet weak var M4HisRepayLogo: UIImageView!
    
    @IBOutlet weak var M4HisRepayName: UILabel!
    
    @IBOutlet weak var M4HisRepayAmount: UILabel!
    
    @IBOutlet weak var M4HisRepayDate: UILabel!
    
    @IBOutlet weak var M4HisRepayDaysDescribe: UILabel!
    
    @IBOutlet weak var M4HisRepayDays: UILabel!
    
    @IBOutlet weak var M4ProrrogaButton: UIButton!
    
    private var repayModel: M4LoanOrderListModel?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        M4ProrrogaButton.layer.cornerRadius = 2
        M4ProrrogaButton.layer.borderWidth = 0.5
        M4ProrrogaButton.layer.borderColor = COLOR_00479D.cgColor
    }
    
    override func M4UpdateDataAndUI() {
        let model = data as? M4LoanOrderListModel
        repayModel = model
        M4HisRepayLogo.sd_setImage(with: URL(string: model?.UcddgfwSXM ?? ""))
        M4HisRepayName.text = model?.LcMnJMQqO
        M4HisRepayAmount.text = "$\(M4String.M4BlankAmount(original: "\(model?.CLC ?? 0)"))"
        let time = (model?.KYaDf ?? 0) / 1000
        let date = Date(timeIntervalSince1970: TimeInterval(time))
        let formatter = DateFormatter()
        formatter.dateFormat = "dd-MM-yyyy"
        let dateString = formatter.string(from: date)
        M4HisRepayDate.text = model?.UEKgEb ?? ""
        M4HisRepayDaysDescribe.text = model?.VgmiIBphBcw ?? 0 >= 0 ? "Día de vencimiento restante" : "Días de retraso"
        M4HisRepayDaysDescribe.textColor = model?.VgmiIBphBcw ?? 0 >= 0 ? COLOR_666666 : COLOR_9D0000
        M4HisRepayDays.text = model?.VgmiIBphBcw ?? 0 >= 0 ? "\(model?.VgmiIBphBcw ?? 0)Días" : "\(abs(model?.VgmiIBphBcw ?? 0))Días"
        M4HisRepayDays.textColor = model?.VgmiIBphBcw ?? 0 >= 0 ? COLOR_333333 : COLOR_9D0000
        M4ProrrogaButton.isHidden = model?.TTkXKqx == 0 ? true : false
    }
    
    override func M4CellTopEdge() -> CGFloat {
        return 10
    }
    
    @IBAction func M4HisRepayAction(_ sender: UIButton) {
        M4CellActionDelegate?.M4CellAction(M4IDAction(actionType: M4HistoryRepayAction.M4HisPagaAction.rawValue, actionData: repayModel?.Jajlc))
        sender.M4EventUnavailable = false
    }
    
    @IBAction func M4ProrrogaAction(_ sender: UIButton) {
        M4CellActionDelegate?.M4CellAction(M4IDAction(actionType: M4HistoryRepayAction.M4ProrrogaAction.rawValue, actionData: repayModel))
        sender.M4EventUnavailable = false
    }
    
    
    
}
